package nil.ed.simulator.request.ui.jfx.event;

import javafx.application.Platform;
import nil.ed.simulator.request.common.QQItem;
import nil.ed.simulator.request.enums.events.EventType;
import nil.ed.simulator.request.event.Event;
import nil.ed.simulator.request.event.bus.EventListener;
import nil.ed.simulator.request.ui.jfx.bean.qq.QQRow;
import nil.ed.simulator.request.ui.jfx.controller.TableViewController;
import nil.ed.simulator.request.ui.jfx.controller.TopControlPaneController;
import nil.ed.simulator.request.ui.jfx.manager.ControllerManager;

import java.util.List;

/**
 * @author lidelin.
 */
public class QqParseSuccessListener implements EventListener {

    @Override
    @SuppressWarnings("unchecked")
    public void onEvent(Event event) {
        List<QQItem> qqItemList = (List<QQItem>) event.getData();
        TableViewController tableViewController = ControllerManager.getTable();
        TopControlPaneController topControlPaneController = ControllerManager.getTop();
        Platform.runLater(() -> {
            tableViewController.getDataList().clear();
            qqItemList.stream().map(qqItem -> {
                tableViewController.resetTable(QQRow.class);
                QQRow qqRow = new QQRow(qqItem);
                qqRow.setQq(qqItem.getQq());
                return qqRow;
            }).forEach(tableViewController.getDataList()::add);
            tableViewController.getTableView().refresh();
            topControlPaneController.configProgress(qqItemList.size(), 0);
        });
    }

    @Override
    public boolean isSupport(Event event) {
        return event.getEventType() == EventType.PARSE_SUCCESS;
    }
}
